﻿using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Reflection;
using Gammar.Digger.SystemFramework;
using Gammar.Digger.Entity;
using Gammar.Digger.IDAL;
using Gammar.Digger.Data;
namespace Gammar.Digger.DALSqlServer
{
    public partial class digger_url
    {
        public int Add(digger_urlEntity entity, string tableName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(string.Format("INSERT INTO {0}(",tableName));
            strSql.Append("url_id,name,url,is_valid)");
            strSql.Append(" VALUES (");
            strSql.Append("@url_id,@name,@url,@is_valid)");
            SqlParameter[] parameters = {
					new SqlParameter("@url_id",SqlDbType.UniqueIdentifier),
					new SqlParameter("@name",SqlDbType.VarChar),
					new SqlParameter("@url",SqlDbType.VarChar),
					new SqlParameter("@is_valid",SqlDbType.Bit)
					};
            parameters[0].Value = entity.url_id;
            parameters[1].Value = entity.name;
            parameters[2].Value = entity.url;
            parameters[3].Value = entity.is_valid;

            return helper.ExecuteNonQuery(strSql.ToString(), parameters);
        }

        public SortList<digger_urlEntity> GetList(string strWhere,string tableName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select url_id,name,url,is_valid");
            strSql.Append(string.Format(" FROM {0} ",tableName));
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            SqlDataReader dr = helper.ExecuteReader(strSql.ToString()) as SqlDataReader;
            SortList<digger_urlEntity> list = new SortList<digger_urlEntity>();
            while (dr.Read())
            {
                digger_urlEntity entity = new digger_urlEntity();
                if (!Convert.IsDBNull(dr["url_id"]))
                {
                    entity.url_id = (Guid)dr["url_id"];
                }
                entity.name = dr["name"].ToString();
                entity.url = dr["url"].ToString();
                if (!Convert.IsDBNull(dr["is_valid"]))
                {
                    entity.is_valid = (bool)dr["is_valid"];
                }
                list.Add(entity);
            }

            dr.Close();
            dr.Dispose();

            return list;
        }
    }
}
